package interview.blockqueue;


class SynchronizedShareData{

    public  int number=0;

    public  void increase(){
        synchronized (this) {
            try {
                while (number != 0) {
                    this.wait();
                }
                number++;
                System.out.println(Thread.currentThread().getName()+":"+number);
                this.notify();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public  void decrease(){
        synchronized (this) {
            try {
                while (number == 0) {
                    this.wait();
                }
                number--;
                System.out.println(Thread.currentThread().getName()+":"+number);
                this.notify();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class SynchronizedConsumerAndProduct {

    public static void main(String[] args) {
        SynchronizedShareData data = new SynchronizedShareData();
        new Thread(()->{
            for (int i=0;i<5;i++){
                data.increase();
            }
        },"A").start();
        new Thread(()->{
            for (int i=0;i<5;i++){
                data.decrease();
            }
        },"B").start();
    }



}

